<?php
class Bookmark extends AppModel {

	var $name = 'Bookmark';

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
      'User' =>
				array('className' => 'User',
						'foreignKey' => 'user_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'counterCache' => ''
				),
        'Post' =>
				array('className' => 'Post',
						'foreignKey' => 'post_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'counterCache' => ''
				),

	);

    function delBookmark($pid, $uid)
    {
     	$this->execute("DELETE FROM bookmarks WHERE user_id = {$uid} AND post_id = {$pid}");
			return true;
    }

		function delByPost($pid) {
      $this->execute("DELETE FROM bookmarks WHERE post_id = {$pid}");
			return true;
		}

		function userBookmarks($uid,$fields,$criteria,$page) {
			$limit=PAGESIZE;
			$offset = $limit * ($page-1);
			return $this->query("
SELECT $fields FROM posts AS Post JOIN bookmarks ON bookmarks.user_id = $uid AND bookmarks.post_id = Post.id LEFT JOIN users AS User ON Post.user_id = User.id LEFT JOIN categories AS Category ON Post.category_id = Category.id ORDER BY $criteria
LIMIT $offset, $limit
			");
		}

}
?>